package b1.scenes.home
{
	import b1.conifg.*;
	import b1.controls.basic.*;
	import b1.net.*;
	import b1.players.*;
	import b1.utils.*;
	
	import com.greensock.TweenLite;
	import com.greensock.easing.Expo;
	
	import flash.display.Sprite;
	import flash.events.*;
	import flash.text.*;

	public class WantedPostDialog extends Sprite
	{
		private var detectiveAnimal:AnimalInfo;
		private var arrestTagertAnimal:AnimalInfo;
		private var battleCase:BattleCaseInfo;
    	private var container:Sprite;
    	//通缉金额
    	private var numBox:NumberSelecter;
    	private var postButton:BasicButton;
    	
		public function WantedPostDialog(detectiveAnimal:AnimalInfo, arrestTagertAnimal:AnimalInfo, battleCase:BattleCaseInfo)
		{
			super();
			this.detectiveAnimal = detectiveAnimal;
			this.arrestTagertAnimal = arrestTagertAnimal;
			this.battleCase = battleCase;
			container = new Sprite();

			//通缉令对话框
			var WantedDialogBgr:Class = ResourceHelper.getInstance().getClassByName("WantedDialogBgr");
			var dialogSprite:Sprite = new WantedDialogBgr();
			container.addChild(dialogSprite);

			//动物头像框
			var animalRect:Sprite = new Sprite();
			animalRect.graphics.lineStyle(2, 0x000000);
			animalRect.graphics.beginFill(0xFFFFFF);
			animalRect.graphics.drawRoundRect(0, 0, 100, 100, 15, 15);
			animalRect.graphics.endFill();
			animalRect.x = 50;
			animalRect.y = 125;
			container.addChild(animalRect);
			
			//动物头像遮罩
			var animalRectMask:Sprite = new Sprite();
			animalRectMask.graphics.lineStyle(2, 0x000000);
			animalRectMask.graphics.beginFill(0xFFFFFF);
			animalRectMask.graphics.drawRoundRect(0, 0, 100, 100, 15, 15);
			animalRectMask.graphics.endFill();
			animalRectMask.x = animalRect.x;
			animalRectMask.y = animalRect.y;
			container.addChild(animalRectMask);
			
			//动物头像
			var animalMc:AnimalMc = new AnimalMc(arrestTagertAnimal.id, 100);
			animalMc.x = 100;
			animalMc.y = 245;
			animalMc.mask = animalRectMask;
			container.addChild(animalMc);

			//标题名字
			var animalName:TextField = BFont.getAnimalFullName(arrestTagertAnimal);
			animalName.x = 170;
			animalName.y = 130;
			container.addChild(animalName);
			var titleText:TextField = new TextField();
			titleText.wordWrap = true;
			titleText.selectable = false;
        	titleText.autoSize = TextFieldAutoSize.LEFT;
			titleText.defaultTextFormat = new TextFormat("微软雅黑", 15, 0x000000);
			titleText.text = "已不知去向。立即通缉？";
			titleText.width = 100;
			titleText.x = 170;
			titleText.y = animalName.y + 20;
			container.addChild(titleText);

			//加载骨头图标
			var PriceImage:Class = ResourceHelper.getInstance().getClassByName("PriceBone");
			var priceImage:Sprite = new PriceImage();
			priceImage.width = 25;
			priceImage.height = 25;
			priceImage.x = 95;
			priceImage.y = 241;
			container.addChild(priceImage);
			
			//通缉金额输入框
			var LeftArrowButton:Class = ResourceHelper.getInstance().getClassByName("TabPagePageUp");
			var RightArrowButton:Class = ResourceHelper.getInstance().getClassByName("TabPagePageDown");
			numBox = new NumberSelecter(LeftArrowButton, RightArrowButton, battleCase.caseBones, 1, battleCase.caseBones * 10, false, 40);
			numBox.x = priceImage.x + priceImage.width + 10;
			numBox.y = priceImage.y;
			container.addChild(numBox);

			//标题名字
			var durationText:TextField = new TextField();
			durationText.selectable = false;
        	durationText.autoSize = TextFieldAutoSize.LEFT;
			durationText.defaultTextFormat = new TextFormat("微软雅黑", 15, 0x000000);
			var levelConfig:LevelConfig = Config.levelConfigs[detectiveAnimal.level - 1];
			var durationHour:Number = levelConfig.wantedPostDuration / 3600;
			durationText.text = "有效期： " + durationHour.toFixed(1) + " 小时 ";
			durationText.x = priceImage.x;
			durationText.y = priceImage.y + priceImage.height + 5;
			container.addChild(durationText);
			//按钮
			var ButtonBgr1:Class = ResourceHelper.getInstance().getClassByName("ButtonBgr1");
			postButton = new BasicButton(ButtonBgr1, "发布");
			postButton.x = 50;
			postButton.y = container.height - postButton.height - 50;
			postButton.addEventListener(MouseEvent.CLICK, onPostButtonClick);
			container.addChild(postButton);
			var cancelButton:BasicButton = new BasicButton(ButtonBgr1, "取消");
			cancelButton.x = postButton.x + postButton.width + 20;
			cancelButton.y = postButton.y;
			cancelButton.addEventListener(MouseEvent.CLICK, onCancelButtonClick);
			container.addChild(cancelButton);
			container.x = -container.width / 2;
			container.y = -container.height / 2;
			addChild(container);
		}
		
		private function onPostButtonClick(event:MouseEvent):void
		{
			var levelConfig:LevelConfig = Config.levelConfigs[detectiveAnimal.level - 1];
			var timeToClose:int = ServerHelper.getInstance().now() + levelConfig.wantedPostDuration;
			postButton.disable();
			ServerHelper.getInstance().postWantedAnimal(battleCase.caseId, numBox.value, timeToClose, onPostWantedComplete);
		}
		
		private function onPostWantedComplete(result:String):void
		{
			if (result == "success")
			{
				//印章
				var WantedPostStamp:Class = ResourceHelper.getInstance().getClassByName("WantedPostStamp");
				var wantedPostStamp:Sprite = new WantedPostStamp();
				wantedPostStamp.x = -100;
				wantedPostStamp.y = -100;
				wantedPostStamp.scaleX = 6;
				wantedPostStamp.scaleY = 6;
				var targetScale:Number = 1;
				container.addChild(wantedPostStamp);
				TweenLite.to(wantedPostStamp, 0.8, {x:180, y:280, scaleX:targetScale, scaleY:targetScale, rotation: -45, ease:Expo.easeIn, onComplete:onPostStampMovieComplete});
			}
			else
			{
				postButton.enable();
			}
		}
		
		private function onPostStampMovieComplete():void
		{
			TweenLite.to(this, 1.5, {delay:1, x:stage.stageWidth, y:0, scaleX:0.01, scaleY:0.01, rotation: 720, onComplete:onPostMovieComplete});
		}
		
		private function onPostMovieComplete():void
		{
			close();
		}
		
		private function onCancelButtonClick(event:MouseEvent):void
		{
			close();
		}
		
		public function close():void
		{
			if (this.parent != null)
			{
				this.parent.removeChild(this);
			}
		}
	}
}